package q71_simplifyPath;

import java.util.Deque;
import java.util.LinkedList;

public class Solution_2 {
    public static void main(String[] args) {
        Solution_2 s = new Solution_2();
        // System.out.println("/../".equals("/../"));
        System.out.println(s.simplifyPath("/a//b////c/d//././/.."));
    }
    /*
    s1依照栈的使用方式是不正确的
    这里应该使用双端队列更为符合我们所需要的数据结构
     */
    public String simplifyPath(String path) {
        // 先将path按照分隔符进行拆分
        String[] names = path.split("/");
        Deque<String> queue = new LinkedList<>();
        for (String name : names) {
            if ("..".equals(name)) {
                if (queue.size() > 0) {
                    queue.pollLast();
                }
            }else if (name.length() > 0 && !".".equals(name)) {
                queue.offer(name);
            }
        }
        System.out.println(queue);
        StringBuilder sb = new StringBuilder();
        if (queue.size() == 0) {
            return "/";
        }else {
            while (queue.size() > 0) {
                sb.append("/");
                sb.append(queue.poll());
            }
        }

        return sb.toString();


    }
}
